<template>
  <div>

    <page-tools>
      <template #left>
        <div class="tips">
          <i class="el-icon-info" />
          <span>共{{ total }}条记录</span>
        </div>
      </template>
      <template #right>
        <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
        <el-button type="danger" size="small" @click="handleDownload">excel导出</el-button>
        <el-button type="primary" size="small" @click="openDialog">新增员工</el-button>
      </template>
    </page-tools>

    <!-- 表单区域 -->
    <el-card style="margin-top: 10px;">
      <el-table border :data="list">
        <el-table-column label="序号" type="index" :index="indexEdit" />
        <el-table-column label="姓名" prop="username" />

        <!-- 员工头像 -->
        <el-table-column label="头像" width="110">
          <template #default="scope">
            <!-- <img :src="scope.row.staffPhoto" alt="" width="80"> -->
            <el-image :src="scope.row.staffPhoto" class="avatar">
              <!-- 具名插槽新语法  图片加载出错的时候自动显示 -->
              <template #error>
                <!-- <i class="el-icon-picture-outline" /> -->
                <img src="@/assets/common/bigUserHeader.png" width="80">
              </template>
            </el-image>
          </template>
        </el-table-column>

        <el-table-column label="工号" prop="workNumber" />
        <!-- <el-table-column label="聘用形式" prop="formOfEmployment" /> -->
        <el-table-column label="聘用形式" prop="formOfEmployment">
          <template #default="{row}">
            {{ formOfEmployment(row.formOfEmployment) }}
          </template>
        </el-table-column>

        <el-table-column label="部门" prop="departmentName" />
        <!-- <el-table-column label="入职时间" width="180" prop="timeOfEntry" /> -->
        <!-- 格式化时间 -->
        <el-table-column label="入职时间" width="180" prop="timeOfEntry">
          <template #default="{row}">
            {{ timeOfEntry(row.timeOfEntry) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="{row}">
            <el-button type="text" size="small" @click="getDetail(row.id)">查看</el-button>
            <el-button type="text" size="small" @click="assignRole(row.id)">分配角色</el-button>
            <el-button type="text" size="small" @click="fetchDelEmploy(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          :current-page="params.page"
          layout="prev, pager, next"
          :total="total"
          style="margin-top: 10px;"
          :page-size="params.size"
          @current-change="CurrentChange"
        />
      </el-row>
    </el-card>

    <!--弹出层 -->
    <add-employee title="新增员工" :visible="dialogVisible" :fetch-list="fetchList" @close-dialog="closeDialog" />
    <!-- 弹出层  分配角色 -->
    <assign-role :show-role-dialog="showRoleDialog" :cur-id="curId" @close-role="closeRoleDialog" />
  </div>
</template>
<script>
// import PageTools from '@/components/PageTools/index'
import { getEmployeeList, delEmployee } from '@/api/employees'
import AddEmployee from '@/views/Employee/component/index'
import dayjs from 'dayjs'
import { getExportData } from '@/utils/excel.js'
import AssignRole from '@/views/Employee/component/assign-role'

export default {
  name: 'Employees',
  // 注册组件
  components: {
    AddEmployee,
    AssignRole

  }, //   components: {
  //     PageTools
  //   }
  data() {
    return {
      // 列表数据
      list: [],
      params: {
        page: 1,
        size: 10
      },
      total: 0, // 总数
      // 新增员工弹框
      dialogVisible: false,

      // 分配角色弹框
      showRoleDialog: false,

      // 当前分配角色员工id
      curId: ''
      // // 可选角色列表数据
      // roleList: [],
      // // 已选角色列表
      // roleIds: []

    }
  },
  //
  mounted() {
    this.fetchList()
  },
  methods: {
    // 获取员工列表
    async fetchList() {
      const { rows, total } = await getEmployeeList(this.params)
      this.list = rows
      this.total = total
    },
    // 修改员工聘用方式数据数字转换为文字数据
    formOfEmployment(type) {
      // 根据返回数字型数据，确认文字类型
      const map = {
        1: '正式',
        2: '非正式'
      }
      // 返回聘用形式
      return map[type]
    },
    // 格式化时间
    timeOfEntry(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 点击翻页，更新数据
    CurrentChange(page) {
      // 更改页码为当前点击页
      this.params.page = page
      // 更新列表数据
      this.fetchList()
    },
    // 自定义列表序号
    indexEdit(index) {
      return (index + 1) + this.params.size * (this.params.page - 1)
    },
    // 删除员工
    fetchDelEmploy(id) {
      this.$confirm('你确认要该员工数据删除么?', '提示').then(async() => {
        await delEmployee(id)
        // 更新列表
        this.fetchList()
        this.$message.success('删除员工成功')
      }).catch(error => {
        console.log(error)
      })
    },
    // 打开弹层
    openDialog() {
      this.dialogVisible = true
    },
    // 关闭弹层
    closeDialog() {
      this.dialogVisible = false
    },
    // 导入文件
    handleDownload() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getEmployeeList(this.params)
        const headerRelation = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        const { data } = getExportData(res.rows, headerRelation)
        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header: Object.keys(headerRelation), // 表头 必填
          data: data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 查看员工详细信息
    getDetail(id) {
      this.$router.push({
        path: '/detail',
        query: {
          id
        }
      })
    },
    assignRole(id) {
      this.showRoleDialog = true
      this.curId = id
    },
    // async openRoleDialog(id) {
    //   // console.log(1)

    // },
    closeRoleDialog() {
      this.showRoleDialog = false
    }

  }
}
</script>
<style >
/* 图片上传进度条样式 */
.avatar {
  position: absolute;
  display: flex;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #fff;
}
</style>
